<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="phone" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="picCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="picUrl" :src=picUrl alt="" @click="getPicCode">
        </div>
        <div class="form-item">
          <input v-model="smsCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode" >
            {{ second === totalSecond ? '获取验证码' : `${second}秒后重新获取` }}
            </button>
        </div>
      </div>

      <div @click="login" class="login-btn">登录</div>
    </div>
  </div>
  </template>

<script>
import { codeLogin, getPicCode, getSmsCode } from '@/api/login'
// import instance from '@/utils/request'; '@/utils/request'
export default {
  name: 'LoginIndex',
  data () {
    return {
      phone: '',
      picCode: '', // 用户输入的图形验证码
      smsCode: '', // 用户输入的短信验证码
      picKey: '', // 将来请求传递的图形验证码唯一标识
      picUrl: '', // 存储请求渲染的图形地址
      totalSecond: 30, // 倒计时总秒数
      second: 30, // 当前秒数,开定时器对second--
      timer: null
    }
  },
  async created () {
    this.getPicCode()
  },
  // 离开页面，清除定时器
  destroyed () {
    clearInterval(this.timer)
  },
  methods: {
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picUrl = base64
      this.picKey = key

      this.$toast.success('获取成功')
    },
    // 校验手机号和图形验证码
    vaildFn () {
      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        this.$toast.fail('手机号格式不正确')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast.fail('图形验证码格式不正确')
        return false
      }
      return true
    },
    // 获取短信验证码
    async getCode () {
      // 1. 判断手机号是否正确
      // 2. 判断图形验证码是否正确
      // 3. 发送请求获取短信验证码
      // 4. 开启定时器
      if (!this.vaildFn()) {
        return false
      }

      if (!this.timer && this.second === this.totalSecond) {
        // 发达请求
        const res = await getSmsCode(this.picCode, this.picKey, this.phone)
        this.$toast.success('短信发送成功')

        console.log(res)
        this.timer = setInterval(() => {
          this.second--
          if (this.second < 1) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond
          }
        }, 1000)
      }
    },

    // 登录
    async login () {
      if (!this.vaildFn()) {
        return false
      }

      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast.fail('短信验证码格式不正确')
        return false
      }
      const res = await codeLogin(this.phone, this.smsCode)
      this.$store.commit('user/setUserInfo', res.data)
      this.$toast.success('登录成功')
      console.log(res)
      // 进行判断，看地址栏有无回跳地址
      // 1、 如果有 => 说明是其它页面跳转过来，需要回跳
      // 2、如果没有 => 说明是直接访问的登录页，直接跳转到首页
      const url = this.$route.query.redirect || '/'
      this.$router.replace(url)
    }

  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

</style>
